const _function = require("../../utils/functionData.js")
Page({
  data: {
    questionList: [
      // {
      //   id: 1,
      //   title: '如何学习JavaScript？',
      //   author: '张三',
      //   content: '我想系统学习JavaScript，有什么推荐的学习路径吗？'
      // },
    ],
    showPopup: false,
    formData: {
      title: '',
      type: getApp().globalData.userInfo.role,
      content: '',
      createBy: getApp().globalData.userInfo.userId,
    }
  },
  onLoad() {
    this.getLearnLists()
  },
  // 获取论坛列表
  getLearnLists(e) {
    console.log('选择时间', getApp().globalData.userInfo);
    const param = {
    }
    _function.getLearnLists(param, this.getLearnListsBack, this)
  },
  getLearnListsBack(res) {
    let dataAll = res.data
    if (dataAll.code == 200) {
      console.log('视频列表', dataAll);
      this.setData({
        questionList: dataAll.rows
      })
      wx.showToast({
        title: dataAll.msg,
        icon: 'none', // 不显示图标
        duration: 1500, // 提示的延迟时间，单位毫秒，默认为 1500
        mask: true, // 是否显示透明蒙层，防止触摸穿透，默认为 `false`
        success: () => {
          // console.log('登录失败提示已显示');
        }
      });
    }
  },
  // 发布问题
  postQuestion: function () {
    this.setData({ showPopup: true });
  },
  // 监听输入框变化
  onFieldChange(e) {
    const field = e.currentTarget.dataset.field; // 获取字段名
    const value = e.detail; // 获取输入值
    this.setData({
      [`formData.${field}`]: value // 更新 data
    });
    // console.log('改变',value,field,this.data.formData);
  },
  // 对话框确认事件
  onDialogConfirm() {
    console.log('确认', this.data.formData);
    this.postLearnLists()
  },
  onClose() {
    console.log('取消');
    this.setData({
      formData: {
        title: '',
        type: getApp().globalData.userInfo.role,
        content: '',
        createBy: getApp().globalData.userInfo.userId,
      }
    })
  },
  postLearnLists() {
    _function.postLearnLists(this.data.formData, this.postLearnListsBack, this)
  },
  postLearnListsBack(res) {
    let dataAll = res.data
    if (dataAll.code == 200) {
      this.getLearnLists()
      this.setData({
        formData: {
          title: '',
          type: getApp().globalData.userInfo.role,
          content: '',
          createBy: getApp().globalData.userInfo.userId,
        }
      })
      wx.showToast({
        title: dataAll.msg,
        icon: 'none', // 不显示图标
        duration: 1500, // 提示的延迟时间，单位毫秒，默认为 1500
        mask: true, // 是否显示透明蒙层，防止触摸穿透，默认为 `false`
        success: () => {
          // console.log('登录失败提示已显示');
        }
      });
    }
  },
  viewQuestionDetail: function (event) {
    const id = event.currentTarget.dataset.id; // 获取 data-id 的值
    const item = event.currentTarget.dataset.item; // 获取data-item 的值
    console.log(item);
    // wx.navigateTo({
    //   url: `/pages/discussion/detail?id=${questionId}`
    // });
    // wx.navigateTo({
    //   url: `/pages/discussiondetail/discussiondetail`
    // });
    wx.navigateTo({
      url: `/pages/discussiondetail/discussiondetail?data=${encodeURIComponent(JSON.stringify(item))}`
    });
  },
});